﻿// owner 蒋家伟

#pragma once

// std
#include <type_traits>

// jsdk
#include "DataDecodecRegisterUtils.h"

namespace jsdk
{
    template<typename DataDecodec>
    class DataDecodecRegisterHelper
    {
        static_assert(std::is_base_of<IDataDecodec, DataDecodec>::value, "数据解码器必须继承IDataDecodec");
    public:
        DataDecodecRegisterHelper(const std::string& agreementHead) {
            jsdk::DataDecodecRegisterUtils::registerCreator(agreementHead, [](void) {return std::make_unique<DataDecodec>(); });
        }
        ~DataDecodecRegisterHelper() {
        }
    };
}

#pragma region 基本定义，会注册到管理器中
#define DATA_DECODEC_DECLARE_INT(CLASSNAME, ...) \
public: \
static const std::string getAgreementHead() \
{ \
    return jsdk::DataDecodecRegisterUtils::getAgreementString_Int<__VA_ARGS__>();\
} \
static jsdk::DataDecodecRegisterHelper< CLASSNAME > s_##CLASSNAME##_RegisterHelper; \
CLASSNAME ();

#define DATA_DECODEC_DECLARE_CHAR(CLASSNAME, ...) \
public: \
static const std::string getAgreementHead() \
{ \
    return jsdk::DataDecodecRegisterUtils::getAgreementString_Char<__VA_ARGS__>();\
} \
static jsdk::DataDecodecRegisterHelper< CLASSNAME > s_##CLASSNAME##_RegisterHelper; \
CLASSNAME ();

#define DATA_DECODEC_DEFINE(NAMESPACE , CLASSNAME) \
jsdk::DataDecodecRegisterHelper < NAMESPACE :: CLASSNAME > NAMESPACE :: CLASSNAME :: s_##CLASSNAME##_RegisterHelper( NAMESPACE :: CLASSNAME :: getAgreementHead());\
NAMESPACE:: CLASSNAME :: CLASSNAME()

#pragma region 基本定义，会注册到管理器中

#pragma region 临时定义，并不会注册到管理器中

#define DATA_DECODEC_DECLARE_INT_TEMPORARY(CLASSNAME, ...) \
public: \
static const std::string getAgreementHead() \
{ \
    return jsdk::DataDecodecRegisterUtils::getAgreementString_Int<__VA_ARGS__>();\
} 

#define DATA_DECODEC_DECLARE_CHAR_TEMPORARY(CLASSNAME, ...) \
public: \
static const std::string getAgreementHead() \
{ \
    return jsdk::DataDecodecRegisterUtils::getAgreementString_Char<__VA_ARGS__>();\
}

#define DATA_DECODEC_DEFINE_TEMPORARY(NAMESPACE , CLASSNAME) \
jsdk::DataDecodecRegisterHelper < NAMESPACE :: CLASSNAME > s_##CLASSNAME##_RegisterHelper( NAMESPACE :: CLASSNAME :: getAgreementHead()); 

#pragma region 临时定义，并不会注册到管理器中
